package com.ryan.smile;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ryan.smile.model.TitleModel;
import com.ryan.smile.model.ViewModel;
import com.ryan.smile.util.FileUtil;

public class ViewActivity extends BaseActivity {

    private int mStage;
    private int mLine;
    
    private TextView mTitle;
    private TextView mBody;
    private Button mLast;
    private Button mNext;
    
    private ScrollView mSv;
    @Override
    protected void doCreate(Bundle savedInstanceState) {
        setContentView(R.layout.view);
        
        mStage = getIntent().getIntExtra("stage", 0);
        mLine = getIntent().getIntExtra("line", 0);
        
        mTitle = (TextView) findViewById(R.id.title);
        mBody = (TextView) findViewById(R.id.body);
        mLast = (Button) findViewById(R.id.last);
        mNext = (Button) findViewById(R.id.next);
        mSv = (ScrollView) findViewById(R.id.sv);
        
        mTitle.setVisibility(View.GONE);
        mLast.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                mLine--;
                load();
            }
        });
        
        mNext.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                mLine++;
                load();
            }
        });
        
        load()  ;
    }
    private void load() {
        TitleModel tm = FileUtil.getIdxMap(this).get(mStage).get(mLine);
        ViewModel vm = FileUtil.getView(this, mStage,tm.getSeek());
        if(vm==null)
            return ;
        
        if(tm.getLastSeek()==-1){
            mLast.setEnabled(false);
            mLast.setBackgroundResource(R.drawable.left_p);
        }else{
            mLast.setEnabled(true);
            mLast.setBackgroundResource(R.drawable.left);
        }
        if(tm.getNextSeek()==-1){
            mNext.setEnabled(false);
            mNext.setBackgroundResource(R.drawable.right_p);
        }else{
            mNext.setEnabled(true);
            mNext.setBackgroundResource(R.drawable.right);
        }
        mBody.setText(vm.getBody()); 
        ((ScrollView)mBody.getParent()).scrollTo(0, 0);
        ((ScrollView)mBody.getParent()).requestLayout();
        ((ScrollView)mBody.getParent()).invalidate();
        mBody.requestLayout();
    }
}